package com.itheima.demo5;

public class Warehouse {
    private int warehouse;

    public Warehouse(int warehouse
) {
        this.warehouse = warehouse;
    }

    // 存放货物的方法
    public synchronized void put() {
        while (warehouse >= 10) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        warehouse ++;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "存入1个单位货物，现在仓库剩余" + warehouse);
        notifyAll();
    }

    // 取出货物的方法
    public synchronized void get() {
        while (warehouse <= 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        warehouse --;
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "取出1个单位货物，现在仓库剩余" + warehouse);
        notifyAll();
    }
}
